package org.todd.healthreport.service.Impl;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.todd.healthreport.service.WeatherService;

@Service
public class WeatherServiceImpl implements WeatherService {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${weatherUrl}")
    private String weatherUrl;

    @Override
    public JSONObject getWeather(String key, String city) {
        ResponseEntity<JSONObject> forEntity = restTemplate.getForEntity(getWeatherUrl(key, city), JSONObject.class);
        JSONObject jsonBody = forEntity.getBody();
        JSONObject result = jsonBody.getJSONObject("result");
        if (result!=null){
            return result;
        }
        return null;
    }

    private String getWeatherUrl(String key,String city){
        return weatherUrl+"?key="+key+"&city="+city;
    }

}
